<?
require_once "File/Find.php";
class ScanAndAdd {

	function ScanAndAdd(){
		return $this;
	}
	
	private function getFilesList($path){
	 
	 $dir = $path;
	 list($directories, $files) = File_Find::maptree($dir);

	 $list = $files;

	 for ($i=0;$i<count($list);$i++) {
	  if(eregi("jpg$", $list[$i])){
  		$list[$i] = "/".preg_replace("/".preg_quote(DOCUMENT_ROOT,"/")."/","",$list[$i]);
	  }
	 }

		return $list;
	}
	
	public function getDirList($path = '/fotobank') {
		
		$dir = DOCUMENT_ROOT.$path;
	 	list($directories, $files) = File_Find::maptree($dir);
		
		return $directories;
	}
	
	public function scan($dir) {
		
		$filesList = $this->getFilesList($dir);
		$articlesEditor = new ArticlesEditor();
		
		for ($i=0;$i<count($filesList);$i++) {
			$fileName = basename($filesList[$i]);
			$articlesEditor->existsImg($filesList[$i]);
			if(!$articlesEditor->exists){
				$articlesEditor->add($fileName, $fileName, $_REQUEST['page_id'], $filesList[$i]);
			}
	 	}
		
		header("location:?page=news&page_id=".$_REQUEST['page_id']);
		
	}
}
?>